(function (root) {
  const loadDefaultAdImg ="https://pic.quanjing.com/sz/98/QJ8146440608.jpg?x-oss-process=style/350h"
  const adUrl = "http://www.wxsdk.com/jsonApi?limit=10&callback=jsonData"
  function getJsPath (jsname = "ad.js") {
    let js = document.scripts
    let jsPath = ""
    for (let i = js.length; i > 0; i--) {
      if (js[i - 1].src.indexOf(jsname) > -1) {
        return js[i - 1].src
      }
    }
    return jsPath
  }

  function getParams (jspath) {
    let urlparse = jspath.split("?")
    if (!urlparse || !urlparse[1]) {
      return
    }
    let parms = urlparse[1].split("&")
    let values = {}
    for (let i = 0; i < parms.length; i++) {
      let pr = parms[i].split("=")
      if (pr.length) {
        values[pr[0]] = pr[1]
      }
    }
    return values
  }
  function randomNum (minNum, maxNum) {
    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)
  }


  function close (e) {
    let txt = txtBtn.innerText
    if (adList.length) {
      let randNum = randomNum(0, adList.length - 1)
      imgInfo.src =adList[randNum]?.img? adList[randNum].img:adList[randNum]
    }

    elem.style.transition = "all 0.3s ease"
    if (txt !== "√") {
      elem.style.innerText = ""
      elem.style.bottom = "-" + width * 0.85 + "px"
      elem.style.width = "60px"
      txtBtn.innerText = "√"
      imgInfo.style.display = "none"
    } else {
      elem.style.bottom = "0px"
      elem.style.width = width + "px"
      txtBtn.innerText = "x"
      imgInfo.style.display = "block"
    }
    e.stopPropagation()
  }

  function toLinik (e) {
    if (adLink) {
      window.location.href = adLink
    }
    e.stopPropagation()
  }


  let jspath = getJsPath()
  let ADconfig = getParams(jspath)


  function addScriptTag (src) {
    let script = document.createElement("script")
    script.onerror = function () {
      console.log("加载失败",loadDefaultAdImg)
      setConfig([loadDefaultAdImg])
      randShowAdvanced()
    }
    script.setAttribute("type", "text/javascript")
    script.src = src
    document.body.appendChild(script)
  };
  function setConfig (data) {
    adList = data
  }

  let width = ADconfig && ADconfig.width ? ADconfig.width : 200
  let height = ADconfig && ADconfig.height ? ADconfig.height : 200
  let position = "fixed"
  let zIndex = 999
  let right = ADconfig && ADconfig.right ? ADconfig.right : 10
  let bottom = ADconfig && ADconfig.bottom ? ADconfig.bottom : 0
  let border = "20px"
  let adImg = ADconfig && ADconfig.adImg ? ADconfig.adImg :loadDefaultAdImg
  let adLink = ADconfig && ADconfig.adLink ? ADconfig.adLink : ""
  let showH5 = ADconfig ? ADconfig.showH5 : false

  let adList = []
  let showRouter = ADconfig ? ADconfig.showRouter : ["login", "error", "404", "dashboard", "article","formDesign"]
  if (!showH5) {
    if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
      return
    }
  }
  let fatherDiv = document.createElement("div")
  let elem = document.createElement("div")
  let txtBtn = document.createElement("div")
  let imgInfo = document.createElement("img")

  window.onload = function () {
    addScriptTag(adUrl)
  }

  window.jsonData = (data) => {

    if (data && data.list) {
      setConfig(data.list)
      randShowAdvanced()

    }
  }
  function randShowAdvanced () {
     if (adList.length) {
        let randNum = randomNum(0, adList.length - 1)
        insertAd(adList[randNum]?.img?adList[randNum].img:adList[randNum])
      }
  }

  function insertAd (myImg = "") {
    if (myImg) {
      adImg = myImg
    }

    fatherDiv.style.position = position
    fatherDiv.style.right = right + "px"
    fatherDiv.style.bottom = bottom + "px"


    elem.style.width = width + "px"
    elem.style.height = height + "px"
    elem.style.zIndex = zIndex
    elem.style.position = position
    elem.style.textAlign = "center"
    elem.style.position = "relative"
    elem.style.backgroundColor = "transparent"
    elem.style.borderRadius = border
    elem.style.display = "block"
    elem.addEventListener("click", toLinik)


    txtBtn.innerText = "x"
    txtBtn.style.position = "absolute"
    txtBtn.style.right = "8px"
    txtBtn.style.top = "10px"
    txtBtn.style.zIndex = 9990
    txtBtn.style.width = "40px"
    txtBtn.style.height = "20px"
    txtBtn.style.textAlign = "center"
    txtBtn.style.color = "#ffffff"
    txtBtn.style.display = "block"
    txtBtn.style.backgroundColor = "rgba(20,20,20,0.6)"
    txtBtn.style.userSelect = "none"
    txtBtn.addEventListener("click", close)


    imgInfo.src = adImg
    imgInfo.style.width = "100%"
    imgInfo.style.height = "100%"
    imgInfo.style.zIndex = 999
    imgInfo.style.borderRadius = border
    imgInfo.style.display = "block"
    imgInfo.style.verticalAlign = "middle"
    imgInfo.style.objectFit = "cover"

    elem.appendChild(txtBtn)
    elem.appendChild(imgInfo)
    fatherDiv.appendChild(elem)
    document.body.appendChild(fatherDiv)
  }
})(window)

